package com.tang.generate.utils

import com.tang.generate.model.req.ReqGenerate
import org.springframework.javapoet.CodeBlock
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter

object GenLocalUtils {


    fun genCodeLocal(reqGenerate: ReqGenerate?, fileName: String, code: String) {
        if (fileName.endsWith(".java")) {
            genJavaCodeLocal(reqGenerate!!.groupId!!, reqGenerate.module!!,  fileName, code)
        }
    }

    /**
     * 生成 java 代码到本地
     */
    private fun genJavaCodeLocal(groupId: String, module: String,  fileName: String, code: String) {
        val file: File;
        if (fileName.contains("controller")) {
            file = File("${System.getProperty("user.dir")}/module/portal/src/main/java/${groupId}/controller/admin/sys/${fileName}")
        }else if (fileName.contains("facade")){
            file = File("${System.getProperty("user.dir")}/module/portal/src/main/java/${groupId}/facade/admin/sys/${fileName}")
        }else if (fileName.contains("Service") && fileName.contains("Impl")){
            file = File("${System.getProperty("user.dir")}/module/service/{$module}src/main/java/${groupId}/impl/${fileName}")
        }else if (fileName.contains("Service") && !fileName.contains("Impl")){
            file = File("${System.getProperty("user.dir")}/module/service/{$module}src/main/java/${groupId}/service/${fileName}")
        }else if (fileName.contains("Mapper")){
            file = File("${System.getProperty("user.dir")}/module/service/{$module}src/main/java/${groupId}/service/${fileName}")
        }else if (fileName.contains("Req")){
            file = File("${System.getProperty("user.dir")}/module/service/{$module}src/main/java/${groupId}/model/req/${fileName}")
        }else if (fileName.contains("Res") ){
            file = File("${System.getProperty("user.dir")}/module/service/{$module}src/main/java/${groupId}/model/res/${fileName}")
        }else {
            file = File("${System.getProperty("user.dir")}/module/service/{$module}src/main/java/${groupId}/domain/${fileName}")
        }
        val bufferedWriter: BufferedWriter = BufferedWriter(FileWriter(file))
        bufferedWriter.write(code)
        bufferedWriter.flush()
        bufferedWriter.close();
    }


}
